package com.sos.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sos.dto.OrderSearch;
import com.sos.entity.BasePage;
import com.sos.entity.dto.DoctorDto;
import com.sos.entity.dto.UserOrder;
import com.sos.entity.po.*;
import com.sos.mapper.DoctorMapper;
import com.sos.mapper.ProcessMapper;
import com.sos.mapper.SubjectMapper;
import com.sos.mapper.UserMapper;
import com.sos.service.ProcessService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author 港某人
 * @date 2021/3/7 20:45
 */
@Service
public class ProcessServiceImpl extends ServiceImpl<ProcessMapper, Object> implements ProcessService {

    @Resource
    SubjectMapper subjectMapper;

    @Resource
    DoctorMapper doctorMapper;

    @Resource
    UserMapper userMapper;

    @Override
    public List<Subject> getAllSubject() {
        return subjectMapper.getAllSubject();
    }

    @Override
    public Page<Doctor> getDoctor(DoctorDto doctorDto) {
        Page<Doctor> page = new Page<>(doctorDto.getPage(), doctorDto.getLimit());
        List<Doctor> doctor = doctorMapper.getDoctorBySubject(page, doctorDto);
        page.setRecords(doctor);
        return page;
    }

    @Override
    public String createOrder(UserOrder userOrder) {
        Account accountById = userMapper.getAccountById(userOrder.getAccount());
        if (accountById == null) {
            return "身份证号未注册";
        }
        if (!accountById.getPassword().equals(userOrder.getPassword())) {
            return "密码错误";
        }
        Order order = new Order(userOrder);
        User user = userMapper.getUserByAccount(userOrder.getAccount());
        if (user != null) {
            order.setUserName(user.getName());
        }
        Doctor doctor = doctorMapper.getDoctorById(userOrder.getDoctorId());
        if (doctor != null) {
            order.setDoctorName(doctor.getName());
        }
        baseMapper.insertOrder(order);
        return "预约成功";
    }

    @Override
    public Page<Order> getOrderToday(BasePage basePage) {
        Page<Order> page = new Page<>(basePage.getPage(), basePage.getLimit());
        LocalDate now = LocalDate.now();
        String startDate = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        String endDate = now.plusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        List<Order> orderToday = baseMapper.getOrderToday(page, startDate, endDate);
        page.setRecords(orderToday);
        return page;
    }

    @Override
    public Page<OrderDetail> getOrderByUserAccount(OrderSearch orderSearch) {
        Page<OrderDetail> page = new Page<>(orderSearch.getPage(), orderSearch.getLimit());
        List<OrderDetail> order = baseMapper.getOrderByUserAccount(page, orderSearch);
        page.setRecords(order);
        return page;
    }

}
